package tt.dz.appserver.util;

import java.math.BigDecimal;

/**
 * 计算2点之间距离算法
 * @author taindejiang
 *
 */
public class CalculateUtil {

	private static final double EARTH_RADIUS = 6378.137;
	private static double rad(double d)
	{
	   return d * Math.PI / 180.0;
	}

	public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
	{
	   double radLat1 = rad(lat1);
	   double radLat2 = rad(lat2);
	   double a = radLat1 - radLat2;
	   double b = rad(lng1) - rad(lng2);
	   double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + 
	    Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
	   s = s * EARTH_RADIUS;
	   BigDecimal  big= new BigDecimal(s);
	   s=big.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	  /* s = Math.round(s * 10000);
	   s=s/10000;*/
	   return s;
	}
	
	public static void main(String[] args) {
		double a=GetDistance(12.312, 31.321, 13.21, 13.21);
		//System.out.println(a);
		double p1=8.42337;
		 /*p1 = (Math.round(p1 * 10000));
		 p1/=10000;*/
		 System.out.println(p1);
		 System.out.println(10.0/3);
		 BigDecimal  big= new BigDecimal(p1);
		 p1=big.setScale(4,   BigDecimal.ROUND_HALF_UP).doubleValue();
		 System.out.println(p1);
		
	}
	
	
}
